home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 13 / CU Amiga Magazine's Super CD-ROM 13 (1997)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1997-08].iso / CUCD / Graphics / Ghostscript / src / jpeg-6a / jcapimin.c < prev    next >
C/C++ Source or Header  |  1996-01-06  |  7KB  |  237 lines

  1. /*
  2.  * jcapimin.c
  3.  *
  4.  * Copyright (C) 1994-1996, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains application interface code for the compression half
  9.  * of the JPEG library.  These are the "minimum" API routines that may be
  10.  * needed in either the normal full-compression case or the transcoding-only
  11.  * case.
  12.  *
  13.  * Most of the routines intended to be called directly by an application
  14.  * are in this file or in jcapistd.c.  But also see jcparam.c for
  15.  * parameter-setup helper routines, jcomapi.c for routines shared by
  16.  * compression and decompression, and jctrans.c for the transcoding case.
  17.  */
  18.  
  19. #define JPEG_INTERNALS
  20. #include "jinclude.h"
  21. #include "jpeglib.h"
  22.  
  23.  
  24. /*
  25.  * Initialization of a JPEG compression object.
  26.  * The error manager must already be set up (in case memory manager fails).
  27.  */
  28.  
  29. GLOBAL(void)
  30. jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
  31. {
  32.   int i;
  33.  
  34.   /* Guard against version mismatches between library and caller. */
  35.   cinfo->mem = NULL;        /* so jpeg_destroy knows mem mgr not called */
  36.   if (version != JPEG_LIB_VERSION)
  37.     ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
  38.   if (structsize != SIZEOF(struct jpeg_compress_struct))
  39.     ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
  40.          (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
  41.  
  42.   /* For debugging purposes, zero the whole master structure.
  43.    * But error manager pointer is already there, so save and restore it.
  44.    */
  45.   {
  46.     struct jpeg_error_mgr * err = cinfo->err;
  47.     MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
  48.     cinfo->err = err;
  49.   }
  50.   cinfo->is_decompressor = FALSE;
  51.  
  52.   /* Initialize a memory manager instance for this object */
  53.   jinit_memory_mgr((j_common_ptr) cinfo);
  54.  
  55.   /* Zero out pointers to permanent structures. */
  56.   cinfo->progress = NULL;
  57.   cinfo->dest = NULL;
  58.  
  59.   cinfo->comp_info = NULL;
  60.  
  61.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  62.     cinfo->quant_tbl_ptrs[i] = NULL;
  63.  
  64.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  65.     cinfo->dc_huff_tbl_ptrs[i] = NULL;
  66.     cinfo->ac_huff_tbl_ptrs[i] = NULL;
  67.   }
  68.  
  69.   cinfo->input_gamma = 1.0;    /* in case application forgets */
  70.  
  71.   /* OK, I'm ready */
  72.   cinfo->global_state = CSTATE_START;
  73. }
  74.  
  75.  
  76. /*
  77.  * Destruction of a JPEG compression object
  78.  */
  79.  
  80. GLOBAL(void)
  81. jpeg_destroy_compress (j_compress_ptr cinfo)
  82. {
  83.   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
  84. }
  85.  
  86.  
  87. /*
  88.  * Abort processing of a JPEG compression operation,
  89.  * but don't destroy the object itself.
  90.  */
  91.  
  92. GLOBAL(void)
  93. jpeg_abort_compress (j_compress_ptr cinfo)
  94. {
  95.   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  96. }
  97.  
  98.  
  99. /*
  100.  * Forcibly suppress or un-suppress all quantization and Huffman tables.
  101.  * Marks all currently defined tables as already written (if suppress)
  102.  * or not written (if !suppress).  This will control whether they get emitted
  103.  * by a subsequent jpeg_start_compress call.
  104.  *
  105.  * This routine is exported for use by applications that want to produce
  106.  * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
  107.  * since it is called by jpeg_start_compress, we put it here --- otherwise
  108.  * jcparam.o would be linked whether the application used it or not.
  109.  */
  110.  
  111. GLOBAL(void)
  112. jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
  113. {
  114.   int i;
  115.   JQUANT_TBL * qtbl;
  116.   JHUFF_TBL * htbl;
  117.  
  118.   for (i = 0; i < NUM_QUANT_TBLS; i++) {
  119.     if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
  120.       qtbl->sent_table = suppress;
  121.   }
  122.  
  123.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  124.     if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
  125.       htbl->sent_table = suppress;
  126.     if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
  127.       htbl->sent_table = suppress;
  128.   }
  129. }
  130.  
  131.  
  132. /*
  133.  * Finish JPEG compression.
  134.  *
  135.  * If a multipass operating mode was selected, this may do a great deal of
  136.  * work including most of the actual output.
  137.  */
  138.  
  139. GLOBAL(void)
  140. jpeg_finish_compress (j_compress_ptr cinfo)
  141. {
  142.   JDIMENSION iMCU_row;
  143.  
  144.   if (cinfo->global_state == CSTATE_SCANNING ||
  145.       cinfo->global_state == CSTATE_RAW_OK) {
  146.     /* Terminate first pass */
  147.     if (cinfo->next_scanline < cinfo->image_height)
  148.       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  149.     (*cinfo->master->finish_pass) (cinfo);
  150.   } else if (cinfo->global_state != CSTATE_WRCOEFS)
  151.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  152.   /* Perform any remaining passes */
  153.   while (! cinfo->master->is_last_pass) {
  154.     (*cinfo->master->prepare_for_pass) (cinfo);
  155.     for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
  156.       if (cinfo->progress != NULL) {
  157.     cinfo->progress->pass_counter = (long) iMCU_row;
  158.     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
  159.     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  160.       }
  161.       /* We bypass the main controller and invoke coef controller directly;
  162.        * all work is being done from the coefficient buffer.
  163.        */
  164.       if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
  165.     ERREXIT(cinfo, JERR_CANT_SUSPEND);
  166.     }
  167.     (*cinfo->master->finish_pass) (cinfo);
  168.   }
  169.   /* Write EOI, do final cleanup */
  170.   (*cinfo->marker->write_file_trailer) (cinfo);
  171.   (*cinfo->dest->term_destination) (cinfo);
  172.   /* We can use jpeg_abort to release memory and reset global_state */
  173.   jpeg_abort((j_common_ptr) cinfo);
  174. }
  175.  
  176.  
  177. /*
  178.  * Write a special marker.
  179.  * This is only recommended for writing COM or APPn markers.
  180.  * Must be called after jpeg_start_compress() and before
  181.  * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
  182.  */
  183.  
  184. GLOBAL(void)
  185. jpeg_write_marker (j_compress_ptr cinfo, int marker,
  186.            const JOCTET *dataptr, unsigned int datalen)
  187. {
  188.   if (cinfo->next_scanline != 0 ||
  189.       (cinfo->global_state != CSTATE_SCANNING &&
  190.        cinfo->global_state != CSTATE_RAW_OK &&
  191.        cinfo->global_state != CSTATE_WRCOEFS))
  192.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  193.  
  194.   (*cinfo->marker->write_any_marker) (cinfo, marker, dataptr, datalen);
  195. }
  196.  
  197.  
  198. /*
  199.  * Alternate compression function: just write an abbreviated table file.
  200.  * Before calling this, all parameters and a data destination must be set up.
  201.  *
  202.  * To produce a pair of files containing abbreviated tables and abbreviated
  203.  * image data, one would proceed as follows:
  204.  *
  205.  *        initialize JPEG object
  206.  *        set JPEG parameters
  207.  *        set destination to table file
  208.  *        jpeg_write_tables(cinfo);
  209.  *        set destination to image file
  210.  *        jpeg_start_compress(cinfo, FALSE);
  211.  *        write data...
  212.  *        jpeg_finish_compress(cinfo);
  213.  *
  214.  * jpeg_write_tables has the side effect of marking all tables written
  215.  * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
  216.  * will not re-emit the tables unless it is passed write_all_tables=TRUE.
  217.  */
  218.  
  219. GLOBAL(void)
  220. jpeg_write_tables (j_compress_ptr cinfo)
  221. {
  222.   if (cinfo->global_state != CSTATE_START)
  223.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  224.  
  225.   /* (Re)initialize error mgr and destination modules */
  226.   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  227.   (*cinfo->dest->init_destination) (cinfo);
  228.   /* Initialize the marker writer ... bit of a crock to do it here. */
  229.   jinit_marker_writer(cinfo);
  230.   /* Write them tables! */
  231.   (*cinfo->marker->write_tables_only) (cinfo);
  232.   /* And clean up. */
  233.   (*cinfo->dest->term_destination) (cinfo);
  234.   /* We can use jpeg_abort to release memory. */
  235.   jpeg_abort((j_common_ptr) cinfo);
  236. }
  237.